// --确认订单--
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    site:{},
    // 优惠券内容
    status:"0",
    getstatus:"",
    msgId:"",
    coupon:"请选择",
    // 选择的洗车卡
    card:"请选择",
    read: false,
    //选择的洗车卡id
    cardId: "请选择",
    //选择的优惠券id
    couponId:"请选择",
    //设备点的头像
    url:"",
    //显示支付方式
    show: false,
    //支付参数
    device_id:"",
    product_iot_id:"",
    ifCreateOrder: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var url = app.globalData.globalUrl;
    var that = this;
    var card= "";
    if(options.card){
      card = JSON.parse(options.card);
      that.setData({
        status: 2
      })
    }
    var coupon = "";
    if (options.coupon) {
      console.log(options.coupon);
      coupon = JSON.parse(options.coupon);
      that.setData({
        status: 1
      })
    }
    this.setData({
      card: card,
      coupon: coupon
    })
    if (coupon == "" && card == "") {
      wx.showModal({
        title: '安全提醒！',
        content: '确保您的车辆安全，请查看停车说明！',
        confirmText: '查看',
        success: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/use/explain/explain',
            })
          }
        }
      })
    }
    var code = wx.getStorageSync("code");
    app.getRequest("api/device/qrcodeDevice", { device_qrcode_id: code},function(e){
      console.log(e)
      if(e.code === 200) {
        that.setData({
          site: e.data,
          url: url,
          has_rolling: e.data.has_rolling,
          device_id: e.data.device_id,
          product_iot_id: e.data.product_iot_id,
          last_use_time: e.data.last_use_time
        })
        // var that = this;
        app.getStatus(that.data.device_id, that.data.product_iot_id, function (stat) {
          that.setData({
            msgId: stat
          })
        })
      } else {
        wx.showToast({
          title: e.message,
          icon:"none"
        })
        setTimeout(function(){
          wx.navigateBack();
        },2000)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // wx.navigateTo({
    //   url: '/use/explain/explain',
    // })
    // wx.showModal({
    //   title: '安全提醒！',
    //   content: '确保您的车辆安全，请查看停车说明！',
    //   confirmText: '查看',
    //   success: (res) => {
    //     if (res.confirm){
    //       wx.navigateTo({
    //         url: '/use/explain/explain',
    //       })
    //     }
    //   }
    // })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.getUserInfoFun();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  /**
   * 选择优惠券
   */
  choseCoupon: function(){
    wx.redirectTo({
      url: '/mine/coupon/coupon?c_id=' + this.data.site.c_id,
    })
  },
  /**
   * 选择洗车卡
   */
  choseCard: function(){
    wx.redirectTo({
      url: '/mine/card/card?use=true',
    })
  },
  /**
   * 阅读协议
   */
  readHttp: function() {
    wx.navigateTo({
      url: '/use/http/http',
    })
  },
  /**
   * 勾选阅读
   */
  read: function() {
    this.setData({
      read: true
    })
  },
  /**
   * 选择支付方式
   */
  pay: function(){
    var that = this;
    var card = this.data.card;
    var read = this.data.read;
    var pay_type;
    if (read == true) {
      if(card){
        if (card.type == 1) {
          pay_type = 2;
        } else if (card.type == 2) {
          pay_type = 3;
        }
        that.getMnstest(pay_type);
        // that.sure(pay_type);
      }else {
        that.setData({
          show: true
        })
      }
    } else {
      wx.showModal({
        title: '提示',
        content: "请选择阅读用户协议",
        showCancel: false
      })
    }
  },
  /**
   * 取消支付
   */
  close: function(){
    this.setData({
      show: false
    })
  },
  getMnstest(pay_type){
    var that = this;
    var status = "";
    var msgId = this.data.msgId;
    var device_id = this.data.device_id;
    var product_iot_id = this.data.product_iot_id;
    if (!msgId) {
      wx.showModal({
        title: '警告',
        content: '设备系统有误！',
        showCancel: false
      })
      return;
    }
    app.getRequestSync("api/device/mnstest", { msgid: msgId, device_id: device_id, product_iot_id: product_iot_id}, function (res) {
      if (res.msgid) {
        that.setData({
          msgId: res.msgid
        });
        return;
      };
      if (res.code == 200) {
        if (res.data.faultInfo == "fault") {
          wx.showModal({
            title: '警告',
            content: '设备故障',
            showCancel: false
          })
          status = 2;
          that.setData({
            getstatus: status
          })
        } else {
          if (res.data.carInPlace == "carNotInPlace") {
            wx.showModal({
              title: '警告',
              content: '请将车停至指定位置',
              showCancel: false
            })
            status = 2;
            that.setData({
              getstatus: status
            })
          } else {
            status = 1;
            that.setData({
              getstatus: status
            })
            that.sure(pay_type)
          }
        }
      } else if (!res.noMessage){
        // wx.showToast({
        //   title: res.message,
        //   icon: "none"
        // })
        status = 1;
        that.setData({
          getstatus: status
        })
        that.sure(pay_type)
      }else{
        app.getStatus(that.data.device_id, that.data.product_iot_id, function (stat) {
          that.setData({
            msgId: stat
          })
        })
      }
    });
  },
  /**
   * 微信支付
   */
  one:function(){
    var that = this;
    this.getMnstest(1);
  },
  /**
   * 余额支付
   */
  four: function () {
    var that = this;
    this.getMnstest(4);
    
  },
  /**
   * 确认支付
   */
  sure: function (data) {
    var ifCreateOrder = this.data.ifCreateOrder;
    if (!ifCreateOrder) {
      return;
    } else {
      this.setData({
        ifCreateOrder: false
      })
    }
    var pay_type = data;
    var u_id = wx.getStorageSync("user").u_id;
    var d_id = this.data.site.id;
    var y_id = "";
    var that = this;
    if(this.data.card !==""){
      y_id = this.data.card.id;
    }
    var coupon_id = "";
    if (this.data.coupon !==""){
      coupon_id = this.data.coupon.c_m_id;
    }
    var params = {
      u_id: u_id,
      d_id: d_id,
      c_m_id: coupon_id,
      y_id: y_id,
      pay_type: pay_type
    }
    var getStatus = that.data.getstatus;
    // app.getStatus(that.data.device_id, that.data.product_iot_id, function (getStatus) {
    if (getStatus == 1) {
      app.getRequest("api/order/createMyOrder",params,function(e){
        if(e.code == 200) {
          var orderId = '';
          if (pay_type == 1) {
            orderId = e.order.id
          } else {
            orderId = e.data.id
          }
          var data = {
            device_id: that.data.device_id,
            product_iot_id: that.data.product_iot_id,
            ordernumber: e.data.ordernumber,
            id: orderId
          }
          var code = wx.getStorageSync("code");
          var redirectUrl = "";
          if(that.data.has_rolling==1){
            redirectUrl = '/use/door/door?ids=' + JSON.stringify(data) + '&qrcode=' + code + '&last_use_time=' + that.data.last_use_time;
          }else{
            redirectUrl = '/use/work/work?ids=' + JSON.stringify(data) + '&qrcode=' + code + '&last_use_time=' + that.data.last_use_time;
          }
          if (pay_type === 1) {
            var res = JSON.parse(e.data);
            wx.requestPayment({
              timeStamp: res.timeStamp,
              nonceStr: res.nonceStr,
              package: res.package,
              signType: res.signType,
              paySign: res.paySign,
              success(res) {
                wx.showToast({
                  title: "支付成功",
                })
                wx.redirectTo({
                  url: redirectUrl,
                })
              },
              fail(res) {
                wx.showModal({
                  title: '',
                  content: "支付失败",
                  showCancel: false
                })
              }
            })
          } else {
            wx.redirectTo({
              url: redirectUrl,
            })
          }
        } else {
          wx.showToast({
            title: e.message,
            icon: "none"
          })
          that.setData({
            ifCreateOrder: true
          })
        }
        })
      }
    // })


      // wx.navigateTo({
      //   url: '/use/explain/explain',
      // })
    
 
  }
})